//Using SDL and standard IO
#include <SDL.h>
#include <SDL_Image.h>
#include <stdio.h>
#include <cstdlib>
#include "ltexture.h"

//Screen dimension constants
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;

//Starts up SDL and creates window
bool init();

//Loads media
bool loadMedia();

//Frees media and shuts down SDL
void close();

//The window we'll be rendering to
SDL_Window* gWindow = NULL;

//The window renderer
SDL_Renderer* gRenderer = NULL;

//Starts up SDL and creates window
bool init()
{
    //Initialization flag
    bool success = true;

    //Initialize SDL
    if( SDL_Init(SDL_INIT_EVERYTHING) != 0 )
    {
        printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
        success = false;
    }
    else
    {
        //Create window
        gWindow = SDL_CreateWindow( "SDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                                  SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);

        if( gWindow == NULL)
        {
            printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError());
            success = false;
        }
        else
        //Create renderer for window
            gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED  );
            if( gRenderer == NULL )
            {
                printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
                success = false;
            }
            else
            {
                printf( "Renderer initialized correctly!\n");
                //Initialize renderer color
                SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );

                //Initialize PNG loading
                int imgFlags = IMG_INIT_PNG;
                if( !( IMG_Init( imgFlags ) & imgFlags ) )
                {
                    printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
                    success = false;
                }
            }
    }
    return success;
}

//Loads media
bool loadMedia()
{
    //Loading success flag
    bool success = true;

    LTexture* gBGTexture = new LTexture( gRenderer );

    gBGTexture->loadFromFile("Sea battle GUIa.png");
    if (gBGTexture == NULL )
    {
        printf( "Unable to load image %s! SDL Error: %s\n", "Sea battle GUIa.png", SDL_GetError());
        success = false;
    }
    else
    {
            printf("YEAH!");
    }
    return success;
}

//Frees media and shuts down SDL
void close()
{
//	//Free loaded images
//	gBGTexture->free();

	//Destroy window
	SDL_DestroyRenderer( gRenderer );
	SDL_DestroyWindow( gWindow );
	gWindow = NULL;
	gRenderer = NULL;

	//Quit SDL subsystems
	IMG_Quit();
	SDL_Quit();
}


int main( int argc, char* args[])
{
    //Start up SDL and create window
    if( !init())
    {
        printf( "Fdailed to initialize!\n");
    }
    else
    {
        //Load media
        if( !loadMedia())
        {
            printf( "Failed to load media!\n");
        }
        else
        {

        //MAIN LOOP
            //Main loop flag
            bool quit = false;

            //Event hander
            SDL_Event e;

            //While application is running
            while( !quit )
            {
                //Handle events on queue
                while( SDL_PollEvent( &e ) != 0)
                {
                    if( e.type == SDL_QUIT)
                    {
                        quit = true;
                    }
                }

				//Clear screen
				SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
				SDL_RenderClear( gRenderer );

//                //Render texture to screen
//                SDL_RenderCopy( gRenderer, gBGTexture, NULL, NULL );

//				//Render background
//				gBGTexture->render( 0, 0 );

                //Update screen
                SDL_RenderPresent( gRenderer );
            }

        }
    }

    //Free resources and close SDL
    close();

    return EXIT_SUCCESS;
}
